package com.tianyalei.wolf.sample.activity.uppai.util;

import java.io.File;
import java.util.List;

/**
 * Created by wolf on 2016/4/20.
 * 批量上传文件
 */
public class UploadBatchUtil {
    private String[] mPaths;
    private String type;
    private List<File> mFiles;

    public UploadBatchUtil(List<File> files, String type) {
        mPaths = new String[files.size()];
        this.type = type;
        this.mFiles = files;
    }

    public void uploadBatchFile(final UploadBatchCallback callback) {
        callback.progress(1);
        for (int i = 0; i < mFiles.size(); i++) {
            final int index = i;
            File file = mFiles.get(i);
            UploadOneUtil.uploadFile(file, type, new UpLoadCallback() {
                @Override
                public void complete(boolean success, String path) {
                    mPaths[index] = path;
                    //传完了
                    if (getEmptyCount() == 0) {
                        callback.success(success, mPaths);
                    } else
                        callback.progress(mFiles.size() - getEmptyCount() + 1);
                }
            });
        }
    }

    private int getEmptyCount() {
        int count = 0;
        for (int i = 0; i < mPaths.length; i++) {
            if (mPaths[i] == null) {
                count++;
            }
        }

        return count;
    }
}
